﻿using EmlViewer.EmlViewerModel;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;

namespace EmlViewer.Viewers
{
    /// <summary>
    /// UI specific helpers
    /// </summary>
    public class ViewerServices
    {
        private readonly List<Window> _windows = new List<Window>();
        internal int WindowsCount { get { return _windows.Count; } }
        private bool _helpIsOpen;
        public bool HelpIsOpen { get { return _helpIsOpen; } }
        private readonly WindowInfo _wi = new WindowInfo();
        public void DisplayHtml(MessageInfo info, string body)
        {
            HtmlViewer viewer = new HtmlViewer{DataContext = info, HtmlContent = body};
            _windows.Add(viewer);
            viewer.Closed += (s, a) => _windows.Remove(s as Window);
            viewer.Topmost = true;
            viewer.Show();
            viewer.Topmost = false;
        }
        public void CloseChildWindows()
        {
            List<Window> tempList = new List<Window>(_windows);
            foreach (Window w in tempList)
            {
                w.Close();
            }
        }
        public void Notify(string message)
        {
            MessageBox.Show(message, Constants.ApplicationTitle, MessageBoxButton.OK, MessageBoxImage.Information);
        }
        public void DisplayHelpAbout()
        {
            Window w = new HelpAboutViewer();
            w.Closed += (s, a) => { _helpIsOpen = false; };
            w.Dispatcher.InvokeAsync(() =>
            {
                _helpIsOpen = true;
                w.ShowDialog(); });
        }
        public void Minimize()
        {
            try
            {
                Window w = Application.Current.MainWindow;
                _wi.Left = (double) w.GetValue(Window.LeftProperty);
                _wi.Top = (double) w.GetValue(Window.TopProperty);
                _wi.Height = (double) w.GetValue(FrameworkElement.HeightProperty);
                _wi.Width = (double) w.GetValue(FrameworkElement.WidthProperty);
                Application.Current.MainWindow.WindowState = WindowState.Minimized;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        public void Restore()
        {
            try
            {
                Application.Current.MainWindow.WindowState = WindowState.Normal;
                Window w = Application.Current.MainWindow;
                w.SetValue(Window.LeftProperty, _wi.Left);
                w.SetValue(Window.TopProperty, _wi.Top);
                w.SetValue(FrameworkElement.HeightProperty, _wi.Height);
                w.SetValue(FrameworkElement.WidthProperty, _wi.Width);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        public void BeginWait(Dispatcher dispatcher)
        {
            dispatcher.InvokeAsync(() => { Application.Current.MainWindow.Cursor = Cursors.Wait; });
        }
        public void EndWait(Dispatcher dispatcher)
        {
            dispatcher.InvokeAsync(() => { Application.Current.MainWindow.Cursor = Cursors.Arrow; });
        }
        public class WindowInfo
        {
            public WindowInfo()
            {
                Left = Top = 0;
                Width = 1000;
                Height = 600;
            }
            public double Left { get; set; }
            public double Top { get; set; }
            public double Width { get; set; }
            public double Height { get; set; }
            public void Coerce()
            {
                Left = Math.Max(0, Math.Min(1000, Left));
                Top = Math.Max(0, Math.Min(1000, Top));
                Width = Math.Max(0, Math.Min(1200, Width));
                Height = Math.Max(0, Math.Min(1000, Height));
            }
        }
    }
}
